package task01;

import java.util.ArrayList;
import java.util.LinkedList;

public class OderMeal {
    //用于储存顾客想点的菜品
    static LinkedList<Dish>historyList = new LinkedList<>();
    //购物车
    static ArrayList<Dish>cartList = new ArrayList<>();
    //菜单
    static ArrayList<Dish>dishes = new ArrayList<>();
    static {
        dishes.add(new Dish("油焖大虾",56));
        dishes.add(new Dish("辣子鸡",43));
        dishes.add(new Dish("鱼香肉丝",18));
        dishes.add(new Dish("臭鳜鱼",22));
        dishes.add(new Dish("麻婆豆腐",48));
    }
    public static void shouwDishes(){
        for(Dish dish : dishes){
            System.out.println("菜品价格："+dish.getName()+"，价格："+dish.getPrice()+"元");
        }
    }
    public static Dish getDishByName(ArrayList<Dish>list,String name){
        for(Dish dish : list){
            if(name.equals(dish.getName())){
                return dish;
            }
        }
        return null;
    }
    public static void addDish2Cart(String name){
        Dish d = getDishByName(dishes,name);
        if(d!=null){
            cartList.add(d);
            System.out.println(d.getName()+"成功加入购物车！");
        }else{
            System.out.println("你输入的："+name+"不存在！");
        }
    }

    public static void delDish4Cart(String name){
        Dish d = getDishByName(cartList,name);
        if(d!=null){
            cartList.remove(d);
            System.out.println(d.getName()+"成功移出购物车！");
            browserHistory(name);
        }else{
            System.out.println("购物车中不存在："+name);
        }
    }

    private static void browserHistory(String name) {
        Dish d = getDishByName(dishes,name);
        if(!historyList.contains(name)){
            if(historyList.size() == 3){
                //删除最早加入的菜品
                historyList.removeLast();
                //将新菜品加入开头
                historyList.addFirst(d);
            }
            if(historyList.size()<3){
                historyList.addFirst(d);
            }
        }
    }

    public static void showCartList(){

    }

}
